package xyz.redtorch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;

import xyz.redtorch.client.service.rpc.RpcClientApiService;
import xyz.redtorch.client.service.rpc.impl.RpcClientApiServiceImpl;
import xyz.redtorch.client.strategy.StrategyEngineService;
import xyz.redtorch.client.strategy.StrategyEngineServiceImpl;
import xyz.redtorch.client.strategy.suit.StrategyDemo;
import xyz.redtorch.client.strategy.suit.zodiac.StrategyAries;
import xyz.redtorch.client.web.http.AuthService;
import xyz.redtorch.client.web.http.AuthServiceImpl;
import xyz.redtorch.client.web.socket.WebSocketClientHandler;
import xyz.redtorch.common.service.impl.FastEventServiceImpl;
import xyz.redtorch.config.SpringContextUtil;

@SpringBootApplication
@Import({ FastEventServiceImpl.class })
public class RtStrategyApplication {

	private static final Logger logger = LoggerFactory.getLogger(RtStrategyApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(RtStrategyApplication.class, args);
		try {
			runStrategy();
		} catch (InterruptedException e) {
			logger.error("InterruptedException ", e);
		}
	}

	private static void runStrategy() throws InterruptedException {
		AuthService authService = SpringContextUtil.getBean(AuthServiceImpl.class);
		boolean login = authService.login();
		if (login) {
			String operatorId = authService.getOperatorId();
			Integer nodeId = authService.getNodeId();
			logger.info("登录成功,操作者ID:{},节点ID:{}", operatorId, nodeId);
			WebSocketClientHandler webSocketClientHandler = SpringContextUtil.getBean(WebSocketClientHandler.class);
			webSocketClientHandler.connectRtWebSocketClient();

			Thread.sleep(2000);

			RpcClientApiService rpcClientApiService = SpringContextUtil.getBean(RpcClientApiServiceImpl.class);
			logger.info("获取账户列表");
			rpcClientApiService.getAccountList(null, null);
			logger.info("获取持仓列表");
			rpcClientApiService.getPositionList(null, null);
			logger.info("获取混合合约列表");
			rpcClientApiService.getMixContractList(null, null);
			logger.info("获取委托列表");
			rpcClientApiService.getOrderList(null, null);
			logger.info("获取成交列表");
			rpcClientApiService.getTradeList(null, null);
			logger.info("获取Tick列表");
			rpcClientApiService.getTickList(null, null);

			String demoStrategyId = "TEST-STRATEGY-ID-000";
			String ariesStrategyId = "TEST-STRATEGY-ID-001";
			StrategyDemo strategyDemo = new StrategyDemo(demoStrategyId);
			StrategyAries strategyAries = new StrategyAries(ariesStrategyId);

			StrategyEngineService strategyEngineService = SpringContextUtil.getBean(StrategyEngineServiceImpl.class);
			strategyEngineService.addStrategy(strategyDemo);
			strategyEngineService.addStrategy(strategyAries);
			strategyEngineService.start();
			
			strategyDemo.initStrategy();
			strategyAries.initStrategy();
			
			strategyDemo.startTrading();
			strategyAries.startTrading(); 

		}
	}

}
